﻿
        function validateTextBox(clientId) {
        	var rejectMessage = $(clientId).val();
        	if (rejectMessage.trim().length == 0){
        		alert("Reject message is not null");
        		return false;
        	}
        	else {
        		return true;
        	}
        }
        
        
        
        function ValidateLineManager() {
        	var checkmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        	var birthdate = document.getElementById("ctl00_PanelContent_AddLineManagerControl1_BirthDateTextBox").value;
        	var fullname = document.getElementById("ctl00_PanelContent_AddLineManagerControl1_FullNameTextBox").value;
        	var email = document.getElementById("ctl00_PanelContent_AddLineManagerControl1_EmailTextBox").value;
        	var phonenumber = document.getElementById("ctl00_PanelContent_AddLineManagerControl1_PhoneNumberTextBox").value;
        	var title = document.getElementById("ctl00_PanelContent_AddLineManagerControl1_TitleTextBox").value;
        	if (birthdate == ""){
        		alert("Birthdate not null");
        		return false;
        	}
        	else if (fullname =="") {
        		alert("FullName not null");
        		return false;
        	}
        	else if (email==""|| checkmail.test(email)==false) {
        		alert("Email failure");
        		return false;
        	}
        	else if (phonenumber=="") {
        		alert("Phonenumber not null");
        		return false;
        	}
        	else if (title=="") {
        		alert("Title not null");
        		return false;
        	}
        	else {
        		return true;
        	}
        }
        
        function ValidateConsultant() {
        	var checkmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        	var birthdate = document.getElementById("ctl00_PanelContent_AddConsultantControl1_BirthDateTextBox").value;
        	var fullname = document.getElementById("ctl00_PanelContent_AddConsultantControl1_FullNameTextBox").value;
        	var email = document.getElementById("ctl00_PanelContent_AddConsultantControl1_EmailTextBox").value;
        	var phonenumber = document.getElementById("ctl00_PanelContent_AddConsultantControl1_PhoneNumberTextBox").value;
        	if (birthdate == ""){
        		alert("Birthdate not null");
        		return false;
        	}
        	else if (fullname =="") {
        		alert("FullName not null");
        		return false;
        	}
        	else if (email==""|| checkmail.test(email)==false) {
        		alert("Email failure");
        		return false;
        	}
        	else if (phonenumber=="") {
        		alert("Phonenumber not null");
        		return false;
        	}
        	else {
        		return true;
        	}
        }
        
        function ForHeaderCheckBox(chk) {
        	var checkBox = document.getElementsByTagName("input");

        	for (var i = 0; i < checkBox.length; i++) {
        		if(checkBox[i].type == "checkbox") {
        			checkBox[i].checked = chk.checked;
        		}
        	}
        }

        function ForItemCheckBox(chk) {
        	var checkBox = document.getElementById("SelectAllCheckBox");
        	
        	if(checkBox.checked == true) {
        		checkBox.checked = chk.checked;
        	}
        }
        

        function isValidate() {
        	var result = true;
        	var index = "start";
        	var isTrue = 0;
        	var isFalse = 0;
        	var isNon = 0;
        	var errorMessage = "No Error";
        	var endDate = document.getElementById("ctl00_PanelContent_TimesheetUserControl1_EndDateBox").value;
        	var startDate = document.getElementById("ctl00_PanelContent_TimesheetUserControl1_StartDateBox").value;
        	$('#tableEdit tr').each(function() {
        		if ($(this).index() >= 2) {
        			var workDate = $(this).find('input[name$="WorkDateTextBox"]').val().trim();
        			var time = $(this).find('input[name$="TimeTextBox"]').val().trim();
        			var description = $(this).find('input[name$="DescriptionTextBox"]').val().trim();
        			if (isDateTime(workDate) && isWorkingTime(time) && isDescription(description)) {
        				if (Date.parse(workDate) > Date.parse(endDate) || Date.parse(workDate) < Date.parse(startDate)) {
        					isFalse += 1;
        					errorMessage = "Invalid work date.\n WorkDate must between "+ startDate + " and "+ endDate;
        				}
        				else {
        					isTrue += 1;
        				    index += "$" + ($(this).index() - 2).toString();
        				}
        			}
        			else if ((isDateTime(workDate) && time.trim().length == 0 && description.trim().length == 0) ||
	        			     ( workDate.trim().length == 0 && time.trim().length == 0 && description.trim().length == 0)) {
        				isNon += 1;
        			}
        			else {
        				isFalse += 1;
    					if (!isDateTime(workDate)) {
    						errorMessage = "Invalid work date. Date format: mm/dd/yyyy \n";
    					}
    					else if (!isWorkingTime(time)) {
    						errorMessage = "Invalid working time. Value must numeric and between 0 - 24 \n";
    					}
    					else if (!isDescription(description)) {
    						errorMessage = "Description is not null \n";
    					}
        			}
        		}
        	});
        	if (isFalse == 0 && isTrue == 0) {
        		result = false;
        	}
        	else if (isFalse == 0 && isTrue != 0) {
        		document.getElementById("ctl00_PanelContent_TimesheetUserControl1_hiddenBox").value = index;
        		result = true;
        	}
        	else if (isTrue == 0 && isFalse != 0) {
        		alert(errorMessage);
        		result = false;
        	}
        	else {
        		var message = "You have " + isFalse + " invalid row(s) in total " + (isFalse + isTrue).toString() + " row(s) needed to add.\n Do you want to continue add new TimeSheet?";
        		var value = confirm(message);
        		if (value == true) {
        			document.getElementById("ctl00_PanelContent_TimesheetUserControl1_hiddenBox").value = index;
        			result = true;
        		}
        		else {
        			result = false;
        		}
        	}
        	
            return result;
        }
        
        function isDescription(string) {
        	if (string.trim().length == 0){
        		return false;
        	}
        	else {
        		return true;
        	}
        }
        
        function isDateTime(date) {
    	var matches = /^(\d{2})[-\/](\d{2})[-\/](\d{4})$/.exec(date);
        if (matches == null) return false;
        var d = matches[2];
        var m = matches[1] - 1;
        var y = matches[3];
        var composedDate = new Date(y, m, d);
        return composedDate.getDate() == d &&
                composedDate.getMonth() == m &&
                composedDate.getFullYear() == y;
        }
        
        function isWorkingTime(num) {
    	    if (num > 0 && num < 24) return true;
    	    else {
    		    return false;
    	    }
        }
